package components.player
{
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	import UIFree.uif_draw;
	
	import components.c_component;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class c_progressBar extends c_component {
		public static const REQUIRECHANGE: String = 'REQUIRECHANGE';
		private var _percent: Number=0;
		public function c_progressBar(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(MouseEvent.CLICK, doClick);
		}
		
		private function doClick(e: Event): void {
			var rect: Rectangle = getBarRect();
			var r: Number = 1 - (rect.width - (mouseX - rect.x)) / rect.width;
			if (r < 0) r = 0;
			else if (r > 1) r = 1;
			dispatchEvent(new ObjectEvent(REQUIRECHANGE, r));
		}
		
		private function getBarRect(): Rectangle {
			var rect: Rectangle = getBorder();
			var space: Number = app.getStyleSpace(style.bar);
			rect.inflate(-space, -space);
			return rect;
		}
		
		override protected function paint(a_styleClass:String, a_method:String= uif_draw.DRAWRECTMETHOD):void {
			super.paint(a_styleClass, a_method);
			if (style.bar) paintBar(style.bar);
		}
		
		public function get percent(): Number {
			return _percent;
		}
		
		public function set percent(a_percent: Number): void {
			_percent = a_percent;
			repaint();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		protected function paintBar(a_style: Object, a_method:String= uif_draw.DRAWRECTMETHOD): void {
			var rect: Rectangle = getBarRect();
			rect.width = _percent * rect.width;
			this[a_style.draw_method?a_style.draw_method:a_method](rect.x, rect.y, rect.width, rect.height, a_style);			
		}
	}
}